<?php
/**
 * Author：ghostsf
 * Blog：www.ghostsf.com
 * Date: 2016/6/16
 */

namespace app\super\controller;
use app\super\validate\NoticeValidate;
use think\Db;
use app\super\validate\AdvValidate;
/**
 * 公告
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class NoticeController extends CommonController
{
    /**
     * 获得团队公告数据
     */
//    public function getDatas()
//    {
//        $rows = $_POST['rows'];
//        $page = $_POST['page'];
//
//        $where = $this->getDataByCampusid($_POST);
//        if($_POST['start']!=''){
//        $where['adv_time'] =array('>=',$_POST['start']);
//        }
//        if($_POST['end']!=''){
//            $where['adv_time'] =array('<=',$_POST['end']);
//        }
//        $course = Db::table("up_team_adv")->alias('a')
//            ->join('up_u_user u','a.user_id=u.user_id','left')->where($where)
//            ->field('a.*,u.user_username')
//            ->limit($rows * ($page - 1), $rows)
//            ->select();
//
//        $total = Db::table("up_team_adv")->alias('a')->where($where)
//            ->join('up_u_user u','a.user_id=u.user_id','left')
//            ->count();
//        $data['total'] = $total;
//        $data['rows'] = $course;
//        return json_encode($course);
//    }
    /**
     * 显示添加页
     */
//    public function addnotice()
//    {
//        $sh ='3';//发布权限
//        $uid = session('loginSession')['user_id'];
//        $whereMap['user_id'] = $uid;
//        $user_id = Db::table('up_team')->where($whereMap)->field('user_id')->find();//查询这个团队的创建人
//
//        if($user_id==''){//查询这个人在团队里面是否有发布权限
//            $whereUp['team_id'] = 1; //TODO 缺少团队ID
//            $whereUp['team_member_user_id'] = $uid;
//            //查询这个人在这个团队权限
//            $team_member_role = Db::table('up_team_members')->where($whereUp)->field('team_member_role')->find();
//
//            $whereRole['team_role'] = $team_member_role['team_member_role'];
//            $whereRole['team_id'] = 1;//TODO 缺少团队ID
//
//            //查询这个在在这个群里面的权限
//            $RoleArray = Db::table('up_team_role')->where($whereRole)->field('team_role_detail')->find();
//            if (strpos($RoleArray['team_role_detail'],$sh)===false) {
//                return 1;
//            }else{
//                return $this->fetch("notice/add");
//            }
//        }else{
//            return $this->fetch("notice/add");
//
//        }
//    }
    /**
     * 添加功能处理
     */
//    public function insert()
//    {
//        $registrationModel = M("team_adv");
//        $validata = new NoticeValidate();
//        if (!$validata->check($_POST)) {
//            $returnData['status'] = 0;
//            $returnData['msg'] = $validata->getError();
//            return json_encode($returnData);
//        } else {
//            $id = $this->getRandChar(32);
//            $data['adv_time'] =date("Y-m-d H:i:s");
//            $data['user_id'] =1;//TODO  缺少人员ID
//            $data['adv_content'] = $_POST['n_content'];
//            $data['adv_id'] = $id;
//            $data['team_id'] = 1;//TODO  缺少团队ID
//            $registrationModel->insert($data);
//            $returnData['status'] = 1;
//            $returnData['msg'] = "成功";
//            return json_encode($returnData);
//        }
//    }
    /**
     * 显示队公告修改页
     */
//    public function updatenotice()
//    {
//
//        $id = input('nid','');
//        $registrationModel = M("team_adv");
//        $notice = $registrationModel->where('adv_id',$id)->find();
//        $this->assign("notice", $notice);
//
//        $sh ='3';//发布权限
//        $uid = session('loginSession')['user_id'];
//        $whereMap['user_id'] = $uid;
//        $user_id = Db::table('up_team')->where($whereMap)->field('user_id')->find();//查询这个团队的创建人
//
//        if($user_id==''){//查询这个人在团队里面是否有发布权限
//            $whereUp['team_id'] = 1; //TODO 缺少团队ID
//            $whereUp['team_member_user_id'] = $uid;
//            //查询这个人在这个团队权限
//            $team_member_role = Db::table('up_team_members')->where($whereUp)->field('team_member_role')->find();
//
//            $whereRole['team_role'] = $team_member_role['team_member_role'];
//            $whereRole['team_id'] = 1;//TODO 缺少团队ID
//
//            //查询这个在在这个群里面的权限
//            $RoleArray = Db::table('up_team_role')->where($whereRole)->field('team_role_detail')->find();
//            if (strpos($RoleArray['team_role_detail'],$sh)===false) {
//                return 1;
//            }else{
//                return $this->fetch("notice/update");
//            }
//        }else{
//            return $this->fetch("notice/update");
//
//        }
//
//    }
    /**
     * 团队公告修改
     */
//    public function update()
//    {
//        $registrationModel = M("team_adv");
//        $validata = new NoticeValidate();
//        if (!$validata->check($_POST)) {
//            $returnData['status'] = 0;
//            $returnData['msg'] = $validata->getError();
//            return json_encode($returnData);
//        } else {
//            $where['adv_id'] = $_POST['adv_id'];
//            $data['adv_time'] =date("Y-m-d H:i:s");
//            $data['user_id'] =1;
//            $data['adv_content'] = $_POST['n_content'];
//            $registrationModel->where($where)->update($data);
//            $returnData['status'] = 1;
//            $returnData['msg'] = "修改成功";
//            return json_encode($returnData);
//        }
//    }
    /**
     * 删除
     */
//    public function deleteByIDs()
//    {
//        $registrationModel = M("team_adv");
//
//        $ids = input('ids');
//        $ids = rtrim($ids, ",");
//        $map['adv_id'] = array('in', $ids);
//        $deleteinfo = $registrationModel->where($map)->delete();
//        if ($deleteinfo) {
//            return json_encode(array("status" => 1, "msg" => "删除成功！"));
//        } else {
//            return json_encode(array("status" => 0, "msg" => "删除失败！"));
//        }
//    }

    /**
     * 获取团队任务列表数据
     */
    public function getTeamTask(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['title_content']!=''){
            $title_content = $_POST['title_content'];
            $where['assignment_name|assignment_content|ut.team_name'] =array('like',"%$title_content%");
        }
        if($_POST['start']!=''){
            $where['assignment_static_time'] =array('>=',$_POST['start']);
        }elseif ($_POST['end']!=''){
            $where['assignment_static_time'] =array('<=',$_POST['end']);

        }elseif ($_POST['start']!=''&&$_POST['end']!=''){
            $where['assignment_static_time'] =array('between',array($_POST['start'],$_POST['end']));

        }
//        if($_POST['end']!=''){
//            $where['assignment_static_time'] =array('<=',$_POST['end']);
//        }

        if($_POST['startTime']!=''){
            $where['assignment_time'] =array('>=',$_POST['startTime']);
        }elseif ($_POST['endTime']!=''){
            $where['assignment_time'] =array('<=',$_POST['endTime']);

        }elseif ($_POST['startTime']!=''&&$_POST['endTime']!=''){
            $where['assignment_time'] =array('between',array($_POST['startTime'],$_POST['endTime']));

        }
//        if($_POST['endTime']!=''){
//            $where['assignment_time'] =array('<=',$_POST['endTime']);
//        }
        $team_assignmentModel = M("team_assignment");
        $course = $team_assignmentModel->alias('a')
                                        ->where($where)
                                        ->join('up_u_user u','u.user_id = a.user_id','left')
                                        ->join('up_team ut','ut.team_id=a.team_id')
                                        ->field('a.*,u.user_username as assignment_user,ut.team_name')
                                        ->order('a.assignment_time desc')
                                        ->limit($rows * ($page - 1), $rows)
                                        ->select();
        foreach ($course as $key=>&$vo){
            $whereMap['tau_id'] = $vo['assignment_id'];
            $totalUser = Db::table("up_team_assignment_user")->where($whereMap)->count();
            $vo['assignment_num'] = $totalUser;
            $vo['assignment_start_end'] = date('Y-m-d',strtotime($vo['assignment_static_time']))."至".date('Y-m-d',strtotime($vo['assignment_end_time']));
        }
        $total = Db::table("up_team_assignment")->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $course;
        return json_encode($data);
    }

    /**
     * 获取团队发布任务的人员信息
     */
    public function getTeamUser(){
        $cid = $_POST['cid'];
        $where['tau_id'] = $cid;
        $data = Db::table('up_team_assignment_user')
            ->alias('a')
            ->where($where)
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->field('u.user_username')
            ->select();
        $this->assign('course',$data);
        return $this->fetch("notice/list");
    }

    /**
     * 显示添加团队任务页面
     */
//    public function addTeamAdv()
//    {
//        //TODo缺少验证是不是团队管理的人员验证
//        $where['team_id'] = 1;//TODO 缺少团队ID
//        $where['team_member_remove'] = 2;
//        $data = Db::table('up_team_members')->alias('a')
//            ->where($where)
//            ->join('up_u_user u','u.user_id=a.team_member_user_id')
//            ->field('u.user_id,u.user_phone,u.user_username')
//            ->select();
//        $this->assign('data',$data);
//        $sh ='3';//发布权限
//        $uid = session('loginSession')['user_id'];
//        $whereMap['user_id'] = $uid;
//        $user_id = Db::table('up_team')->where($whereMap)->field('user_id')->find();//查询这个团队的创建人
//        if($user_id==''){//查询这个人在团队里面是否有发布权限
//            $whereUp['team_id'] = 1; //TODO 缺少团队ID
//            $whereUp['team_member_user_id'] = $uid;
//            //查询这个人在这个团队权限
//            $team_member_role = Db::table('up_team_members')->where($whereUp)->field('team_member_role')->find();
//
//            $whereRole['team_role'] = $team_member_role['team_member_role'];
//            $whereRole['team_id'] = 1;//TODO 缺少团队ID
//
//            //查询这个在在这个群里面的权限
//            $RoleArray = Db::table('up_team_role')->where($whereRole)->field('team_role_detail')->find();
//            if (strpos($RoleArray['team_role_detail'],$sh)===false) {
//                return 1;
//            }else{
//                return $this->fetch("notice/addTeamAdv");
//            }
//        }else{
//            return $this->fetch("notice/addTeamAdv");
//        }
//
//    }

    /***
     * 添加团队任务功能
     */
//    public function insertTeamAdv(){
//        $uid = session('loginSession')['user_id'];
//
//        $user = input('user/a');
//        if(count($user)<=0){
//            $returnData['status'] = 0;
//            $returnData['msg'] ="选择的人员不能为空";
//            return json_encode($returnData);
//        }
//        $validata = new AdvValidate();
//        if(!$validata->check($_POST)){
//            $returnData['status'] = 0;
//            $returnData['msg'] =$validata->getError();
//            return json_encode($returnData);
//        }else{
//            $data['assignment_id'] = $this->getRandChar(32);
//            $data['assignment_name'] = $_POST['assignment_name'];
//            $data['assignment_content'] = $_POST['n_content'];
//            $data['assignment_static_time'] = $_POST['start'];
//            $data['assignment_end_time'] = $_POST['end'];
//            $data['assignment_time'] = date('Y-m-d H:i:s');
//            $data['user_id'] = $uid;
//            $data['team_id'] = 1;//TODO 缺少团队ID
//            $res = Db::table('up_team_assignment')->insert($data);
//            foreach ($user as $key=>$vo){
//                $data_user['user_id'] = $vo;
//                $data_user['tau_id'] = $data['assignment_id'];
//                Db::table('up_team_assignment_user')->insert($data_user);
//
//            }
//            if ($res) {
//                return json_encode(array("status" => 1, "msg" => "添加成功！"));
//            } else {
//                return json_encode(array("status" => 0, "msg" => "添加失败！"));
//            }
//        }
//    }
}